<template>
  <view>
    <uni-popup ref="inputDialog" type="dialog">
      <uni-popup-dialog
          ref="inputClose"
          mode="input"
          :title="title"
          @close="close()"
          @confirm="dialogInputConfirm">
        <view>
          <uni-forms ref="baseForm" :modelValue="formData" :rules="rules">
            <uni-forms-item label="机台编号" required labelWidth="150rpx" name="devNum">
              <uni-easyinput v-model="formData.devNum" placeholder="请填写机台编号"/>
            </uni-forms-item>

            <uni-forms-item label="机台区域" labelWidth="150rpx" name="location">
              <uni-easyinput type="textarea" v-model="formData.location" placeholder="请填写机台区域" maxlength="32"/>
            </uni-forms-item>
            <uni-forms-item label="厂区" labelWidth="150rpx" name="areaId">
              <view class="borerr">
                <uni-data-select v-model="formData.areaId" :localdata="range" :clear="true"></uni-data-select>
              </view>
            </uni-forms-item>
          </uni-forms>
        </view>
      </uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script>
import {areaList, getBySn, updateBySn} from '@/api/company/device.js'

export default {
  data() {
    return {
      title: '',
      devSn: '',
      formData: {
        areaId: 0,
        devNum: null,
        location: ''
      },
      companyId: this.$store.getters.companyId,
      rules: {
        devNum: {
          rules: [
            {required: true, errorMessage: '请填写机台编号'},
			{
			  maxLength: 16,
			  errorMessage: '不超过 {maxLength} 个字符',
			}
			// {
			// 	validateFunction: function (rule, value, data, callback) {
			// 	  if (value !== '') {
			// 	    var reg = /^[1-9]\d{0,3}$/;
			// 	    if (!reg.test(value)) {
			// 	      callback('请输入数字');
			// 	    }
			// 	  } else {
			// 	    callback();
			// 	  }
			// 	}
			// }
          ]
        },
        location: {
          rules: [
            {
              maxLength: 30,
              errorMessage: '不超过 {maxLength} 个字符',
            }
          ]
        },
      },
      range: []
    }
  },
  beforeUpdate() {
    // this.$refs.baseForm.setRules(this.rules)
  },
  methods: {

    close() {
      this.range = []
      this.formData = {
        areaId: 0,
        devNum: null,
        location: ''
      }
    },

    dialogInputConfirm(val) {
      this.$refs.baseForm.validate().then(res => {
        let data = {
          ...this.formData,
          devSn: this.devSn,
          companyId: this.companyId
        }
		
        updateBySn(data).then(res => {
          if (res.data === 1) {
            this.$modal.msgSuccess("激活成功")
            this.$emit('getList')
            this.$refs.inputDialog.close()
          } else if (res.data === 3) {
            this.$modal.msgError("编号已存在！")
          } else {
            this.$modal.msgSuccess("设置成功")
            this.$emit('getList')
            this.$refs.inputDialog.close()
          }
        })
      }).catch(err => {
      })
    },

    open(sn) {
      this.devSn = sn
      if (sn != null && sn.length != 14) {
        this.$modal.msgError("无效的sn号")
        return
      }
      //先根据sn查询该机台的位置信息
      getBySn({devSn: this.devSn}).then(res => {

        if (res.data == null) {
          this.title = '激活'
        } else {
          if (res.data.companyId !== this.companyId) {
            wx.showToast({
              title: '该机台不属于本公司',
              icon: 'none',
              duration: 3000
            })
            return
          } else {
            this.title = '设置机台信息'
            let area = _.parseInt(res.data.areaId)
            this.formData = res.data
            this.formData.areaId = area
          }
        }
        this.$refs.inputDialog.open()
        areaList({companyId: this.$store.getters.companyId}).then(res => {
          res.data.forEach(t => {
            this.range.push({text: t.text, value: t.id})
          })
        })

      })
    }
  },
}

</script>

<style scoped lang="scss">

.borerr {
  border: 1px solid #e5e5e5;
}
</style>